package de.karlNet.sambamanager.view.beans.forms;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;

public abstract class ViewBean implements IViewBean {
	private boolean show = false;

	@Autowired
	private ViewBeanBucket viewBeanBucket;

	@Override
	public boolean isShow() {
		return show;
	}

	@Override
	public void setShow(boolean show) {
	
		List<IViewBean> otherBeansMorphed = new ArrayList<IViewBean>(
				this.viewBeanBucket.otherBeans);
		otherBeansMorphed.remove(this);
		for (IViewBean otherBean : this.viewBeanBucket.otherBeans) {
			if (!this.equals(otherBean)) {
				System.out.println("hiding " + otherBean);
				otherBean.hide();
			}
		}
		this.show = show;
	}

	public void hide() {
		this.show = false;
	}
	
	public void showErrorMessage(String message) {
		FacesContext.getCurrentInstance().addMessage(
				null,
				new FacesMessage(FacesMessage.SEVERITY_ERROR,
						"An error occured",
						message));
	}

	public void showErrorMessage(Exception e) {
		this.showErrorMessage(e.getMessage());
	}
}
